cmake_minimum_required(VERSION 3.16)
project(QTVisionTest LANGUAGES CXX)

# 设置 C++ 标准为 C++17，并且强制要求该标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# ---------------------- 编码设置 ----------------------
#MSVC 编译器下设置源码和执行字符集为 UTF-8   对应的是QT5的版本
if(MSVC)
    add_compile_options(/source-charset:utf-8 /execution-charset:utf-8)
else()
# 非 MSVC（如 GCC/Clang）设置输入输出字符编码为 UTF-8
    add_compile_options(-finput-charset=UTF-8 -fexec-charset=UTF-8)
endif()
# ---------------------- Qt 配置 ----------------------
# 启用 Qt 自动生成 moc、uic 和 rcc 文件功能
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
# 指定 Qt 安装路径，方便 find_package 找到 Qt 库
set(CMAKE_PREFIX_PATH "E:/Qt/5.15.0/msvc2019_64")
find_package(Qt5 REQUIRED COMPONENTS Core Gui Widgets Sql Network  Qml Quick Multimedia MultimediaWidgets)
find_package(Qt5Charts REQUIRED)
# ---------------------- ONNX Runtime 配置 ----------------------
set(ONNXRUNTIME_DIR "E:/onnxruntime-win-x64-1.22.0")
set(ONNXRUNTIME_INCLUDE_DIR "${ONNXRUNTIME_DIR}/include")
set(ONNXRUNTIME_LIB "${ONNXRUNTIME_DIR}/lib/onnxruntime.lib")
set(ONNXRUNTIME_DLL "${ONNXRUNTIME_DIR}/lib/onnxruntime.dll")
# ---------------------- ZLIB 配置 ----------------------
set(ZLIB_INCLUDE_DIR "E:/vcpkg/installed/x64-windows/include")
set(ZLIB_LIBRARY "E:/vcpkg/installed/x64-windows/lib/zlib.lib")
find_package(ZLIB REQUIRED)
# ---------------------- OpenCV 配置 ----------------------
# 手动指定 libaec 和 SZIP 路径（OpenCV 的依赖）
set(libaec_ROOT "E:/vcpkg/installed/x64-windows")
set(libaec_LIBRARY "${libaec_ROOT}/lib/libaec.lib")
set(libaec_INCLUDE_DIR "${libaec_ROOT}/include")
set(SZIP_LIBRARY "${libaec_ROOT}/lib/szip.lib")
set(SZIP_INCLUDE_DIR "${libaec_ROOT}/include")
# 显式设置 LibArchive 路径
set(LibArchive_ROOT "E:/vcpkg/installed/x64-windows")
find_path(LibArchive_INCLUDE_DIR archive.h PATHS "${LibArchive_ROOT}/include")
find_library(LibArchive_LIBRARY NAMES archive PATHS "${LibArchive_ROOT}/lib")
# 验证 LibArchive 路径是否有效
if(LibArchive_INCLUDE_DIR AND LibArchive_LIBRARY)
    message(STATUS "✅ Found LibArchive: ${LibArchive_LIBRARY}")
else()
    message(FATAL_ERROR "❌ LibArchive not found! Check vcpkg installation.")
endif()
# 显式查找 LibArchive
find_package(LibArchive REQUIRED)
# 显式设置 TIFF 路径，解决 OpenCV 配置时 TIFF 缺失的问题
set(TIFF_INCLUDE_DIR "E:/vcpkg/installed/x64-windows/include")
set(TIFF_LIBRARY "E:/vcpkg/installed/x64-windows/lib/tiff.lib")
find_package(TIFF REQUIRED)
# ---------------------------- CURL 配置 ----------------------------
set(CURL_INCLUDE_DIR "E:/vcpkg/installed/x64-windows/include")
set(CURL_LIBRARY "E:/vcpkg/installed/x64-windows/lib/libcurl.lib")
include_directories(${CURL_INCLUDE_DIR})
link_libraries(${CURL_LIBRARY})
if (EXISTS ${CURL_INCLUDE_DIR}/curl/curl.h)
    message(STATUS "✅ Found curl.h in ${CURL_INCLUDE_DIR}")
else()
    message(FATAL_ERROR "❌ curl.h not found in ${CURL_INCLUDE_DIR}")
endif()
find_package(CURL REQUIRED) #如果你使用了 vcpkg 安装的 curl，那么推荐直接使用：
# 查找 OpenCV，并指定需要的模块
find_package(OpenCV REQUIRED COMPONENTS core imgproc highgui photo xfeatures2d features2d)
message(STATUS "OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV_LIBS: ${OpenCV_LIBS}")




# ---------------------- 构建目标函数 ----------------------
function(configure_target target_name)
    # 添加可执行文件，${ARGN} 是传入的源码文件列表
    add_executable(${target_name} ${ARGN})
    # 添加头文件搜索路径
    target_include_directories(${target_name} PRIVATE
            ${OpenCV_INCLUDE_DIRS}
            ${ONNXRUNTIME_INCLUDE_DIR}
            ${ZLIB_INCLUDE_DIR}
    )
    # 添加库搜索路径（仅 ONNX Runtime 需要单独指定）
    target_link_directories(${target_name} PRIVATE
            "${ONNXRUNTIME_DIR}/lib"
    )
    # 链接库
    target_link_libraries(${target_name} PRIVATE
            ${OpenCV_LIBS}
            ${ZLIB_LIBRARY}
            ${ONNXRUNTIME_LIB}
            ${LibArchive_LIBRARY}   # LibArchive 库
            Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Sql Qt5::Network Qt5::Charts Qt5::Qml Qt5::Quick Qt5::Multimedia Qt5::MultimediaWidgets
    )
    target_include_directories(${target_name} PRIVATE ${CURL_INCLUDE_DIRS})
    target_link_libraries(${target_name} PRIVATE CURL::libcurl)
    # 指定 C++ 标准
    target_compile_features(${target_name} PRIVATE cxx_std_17)
    # 编译后复制 onnxruntime.dll 到可执行文件目录
    add_custom_command(TARGET ${target_name} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${ONNXRUNTIME_DLL}"
            "$<TARGET_FILE_DIR:${target_name}>"
            COMMENT "📦 正在复制 onnxruntime.dll..."
    )
    # Windows 平台下，使用 windeployqt 部署 Qt 运行时 DLL
    #if(WIN32)  Qt5版本
    #    add_custom_command(TARGET ${target_name} POST_BUILD
    #            COMMAND "${CMAKE_PREFIX_PATH}/bin/windeployqt.exe"
    #            # --no-angle
    #            --release
    #            "$<TARGET_FILE_DIR:${target_name}>"
    #            COMMENT "🔄 正在部署 Qt 运行时 DLL..."
    #    )
    #endif()
    if(WIN32)
        add_custom_command(TARGET ${target_name} POST_BUILD
                COMMAND "${CMAKE_PREFIX_PATH}/bin/windeployqt.exe"
                --release
                "$<TARGET_FILE:${target_name}>"
                COMMENT "🔄 正在部署 Qt 运行时 DLL..."
        )
    endif()

endfunction()
# ---------------------- 具体目标配置 -------------------------------------------------------
configure_target(MainExec src/main.cpp)
configure_target(CreateRoi
src/QPlugins/D1ImageProcessing/CreateRoi/main.cpp
src/QPlugins/D1ImageProcessing/CreateRoi/frmCreateRoi.h
src/QPlugins/D1ImageProcessing/CreateRoi/frmCreateRoi.cpp
src/QPlugins/D1ImageProcessing/CreateRoi/sub_res.qrc)


##########设置一个组件案例代码，方便直接的复制使用开始#######################################
## 此处的代码并不涉及项目，只是方便未来的组件复用的相关操作

## 此处我的设想是我点击了一个按钮,我不能直接的停手不进行操作，我需要对按钮的绑定实现异步绑定方便计划的同时进行
configure_target(MyDialogUID2
        src/Example/MyDialogUID2/main.cpp
        src/Example/MyDialogUID2/mydialog.h
        src/Example/MyDialogUID2/mydialog.cpp
        src/Example/MyDialogUID2/UI/MyDialogUI.h
        src/Example/MyDialogUID2/UI/MyDialogUI.cpp)

# 开始MyDialogUI的布局设计
configure_target(MyDialogUI
        src/Example/MyDialogUI/main.cpp
        src/Example/MyDialogUI/mydialog.h
        src/Example/MyDialogUI/mydialog.cpp
        src/Example/MyDialogUI/UI/MyDialogUI.h
        src/Example/MyDialogUI/UI/MyDialogUI.cpp)

# 开始验证Qt槽函数的功能与实现操作
configure_target(ExampleQtSingl
        src/Example/ExampleQtSingl/main.cpp
        src/Example/ExampleQtSingl/mainwindow.h
        src/Example/ExampleQtSingl/mainwindow.cpp
        src/Example/ExampleQtSingl/mydialog.h
        src/Example/ExampleQtSingl/mydialog.cpp)

# 开始验证Qt之间的指针操作
configure_target(ExampleQtZhiZhen
        src/Example/ExampleQtZhiZhen/main.cpp
        src/Example/ExampleQtZhiZhen/mainwindow.h
        src/Example/ExampleQtZhiZhen/mainwindow.cpp
        src/Example/ExampleQtZhiZhen/mydialog.h
        src/Example/ExampleQtZhiZhen/mydialog.cpp)

## 开始饿了么组件的按钮复现
configure_target(ElementQtQssButton
        src/Element/ElementQtQss/Button/main.cpp
        src/Element/ElementQtQss/Button/ElButton.h
        src/Element/ElementQtQss/Button/ElButton.cpp)

# 开始布局操作
configure_target(LayoutDemo
src/Element/ElementQtQss/Layout/LayoutDemo/main.cpp
src/Element/ElementQtQss/Layout/LayoutDemo/layoutdemo.h
src/Element/ElementQtQss/Layout/LayoutDemo/layoutdemo.cpp)

configure_target(DraggableTextEdit
src/Example/DraggableTextEdit/MainWindow.h
src/Example/DraggableTextEdit/MainWindow.cpp
src/Example/DraggableTextEdit/main.cpp
src/Example/DraggableTextEdit/DraggableTextEdit.h
src/Example/DraggableTextEdit/DraggableTextEdit.cpp)

configure_target(FloatTextEdit
src/Example/FloatTextEdit/FloatTextEdit.h
src/Example/FloatTextEdit/FloatTextEdit.cpp
src/Example/FloatTextEdit/main.cpp)
##########设置一个组件案例代码，方便直接的复制使用结束#######################################

###########################################设置一个Qt+opencv算法开始#####################################
configure_target(NewOpencvGUI
        src/NewOpencvGUI/main.cpp
        src/NewOpencvGUI/LoginDeal/LoginFormandRegister.h
        src/NewOpencvGUI/LoginDeal/LoginFormandRegister.cpp
        src/NewOpencvGUI/resource/Resource.qrc
        src/NewOpencvGUI/MianCode/MianCode.h
        src/NewOpencvGUI/MianCode/MianCode.cpp
        src/NewOpencvGUI/MianCode/MianHomeUI/TopWidgetUI/TopWidgetUI.h
        src/NewOpencvGUI/MianCode/MianHomeUI/TopWidgetUI/TopWidgetUI.cpp
        src/NewOpencvGUI/MianCode/MianHomeUI/BottomWidgetUI/BottomWidgetUI.h
        src/NewOpencvGUI/MianCode/MianHomeUI/BottomWidgetUI/BottomWidgetUI.cpp
        src/NewOpencvGUI/MianCode/MianHomeUI/LeftWidgetUI/LeftWidgetUI.h
        src/NewOpencvGUI/MianCode/MianHomeUI/LeftWidgetUI/LeftWidgetUI.cpp
        src/NewOpencvGUI/MianCode/MianHomeUI/CenterWidgetUI/CenterWidgetUI.h
        src/NewOpencvGUI/MianCode/MianHomeUI/CenterWidgetUI/CenterWidgetUI.cpp
        src/NewOpencvGUI/MianCode/MianHomeUI/RightWidgetUI/RightWidgetUI.h
        src/NewOpencvGUI/MianCode/MianHomeUI/RightWidgetUI/RightWidgetUI.cpp
        src/NewOpencvGUI/OpencvDeal/GeometricTransformer/GeometricTransformer.h
        src/NewOpencvGUI/OpencvDeal/GeometricTransformer/GeometricTransformer.cpp
        src/NewOpencvGUI/OpencvUtils/OpencvUtils.h
        src/NewOpencvGUI/OpencvUtils/OpencvUtils.cpp
        src/NewOpencvGUI/OpencvDeal/Filtering_and_smoothing/FilterUtils.h
        src/NewOpencvGUI/OpencvDeal/Filtering_and_smoothing/FilterUtils.cpp
        src/NewOpencvGUI/OpencvDeal/MorphologyUtils/MorphologyUtils.h
        src/NewOpencvGUI/OpencvDeal/MorphologyUtils/MorphologyUtils.cpp
        src/NewOpencvGUI/OpencvDeal/EdgeDetector/EdgeDetector.h
        src/NewOpencvGUI/OpencvDeal/EdgeDetector/EdgeDetector.cpp
)
###########################################设置一个Qt+opencv算法结束#####################################